package org.udr.query;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.udr.datamodel.Association;
import org.udr.datamodel.Attribute;
import org.udr.datamodel.Entity;
import org.udr.datamodel.Property;

public class QueryHelper {

	public Criteria createCriteria(Session session, Query query) {
		Entity rootEntity = query.getRootEntity();
		Criteria rootCriteria = session.createCriteria(query.getRootEntity().getName());
		ProjectionList projectionList = Projections.projectionList();
		processProjections(rootEntity,rootCriteria,projectionList); 
		rootCriteria.setProjection(projectionList);
		return rootCriteria;
		
	}

	private void processProjections(Entity rootEntity, Criteria rootCriteria, ProjectionList projectionList) {
		for (Attribute attribute: rootEntity.getAttributes()) {
			if (attribute instanceof Property) {
				Projection projection = Projections.property(attribute.getName());
				projectionList.add(projection);
			} else if (attribute instanceof Association) {
				Association association  = (Association) attribute;
				
			}
		}
	}

}
